<template>
  <view class="content pb-50">
    <view class="top-block pt-20 pb-20">
      <view class="search-block pl-20 pr-20">
        <u-search placeholder="请输入风险点名称" v-model="params.riskPoint" action-text="筛选" :action-style="{color: '#3F99F7', fontWeight: '500'}" @custom="openPopup" @search="submit()"></u-search>
      </view>
      <view class="check-block pt-20 pl-20 pr-20">
        <view class="check-item">责任单位：{{responsibleIdName ? responsibleIdName : '---'}}</view>
        <view class="check-item">责任人：{{directPersonIdName ? directPersonIdName : '---'}}</view>
      </view>
    </view>
    <view v-if="JSON.stringify(list) != '[]'" class="list-block pl-20 pr-20">
      <view class="item-block pd-20 mt-20" v-for="(item, index) in list" :key="index" @click="chooseItem(item)">
        <view class="info-block">
          <view class="item-title">{{item.riskPoint}}</view>
          <!-- <view class="state-block">{{item.riskType == 1 ? '基础管理' : item.riskType == '2' ? '作业活动' : '设备设施'}}</view> -->
        </view>
        <view class="item-info mt-10">部门：{{item.companyName}}</view>
      </view>
    </view>
    <view v-else class="empty-block">
      <u-empty text="暂无数据" mode="data" icon-size="80"></u-empty>
    </view>
    <u-popup :show="popupState" mode="top" :closeOnClickOverlay="true" :customStyle="{marginTop: '140rpx'}" @close="closePopup">
      <view class="popup-block pd-20">
        <view class="popup-item pt-20 pb-20 border-b-common">
          <view class="item-label">责任单位</view>
          <view class="item-select" @click="openPicker('responsible')">
            <text v-if="responsibleIdName" class="mr-10">{{responsibleIdName}}</text>
            <text v-else class="item-default-text mr-10">请选择责任单位</text>
            <u-icon name="arrow-right" size="26rpx" color="#CCCCCC"></u-icon>
          </view>
        </view>
        <view class="popup-item pt-20 pb-20 border-b-common">
          <view class="item-label">责任人</view>
          <view class="item-select" @click="openPicker('directPerson')">
            <text v-if="directPersonIdName" class="mr-10">{{directPersonIdName}}</text>
            <text v-else class="item-default-text mr-10">请选择责任人</text>
            <u-icon name="arrow-right" size="26rpx" color="#CCCCCC"></u-icon>
          </view>
        </view>
        <view class="button-block pt-20">
          <u-button type="info" text="重置" :custom-style="{width: '340rpx', backgroundColor: '#FFFFFF !important'}" @click="resetForm()"></u-button>
          <u-button type="primary" text="确认" :custom-style="{width: '340rpx'}" @click="submit()"></u-button>
        </view>
      </view>
    </u-popup>
    <u-picker :show="directPersonIdState" :columns="directPersonList" keyName="label" @confirm="pickerConfirm('directPersonId', $event)" @cancel="pickerCancel('directPersonId')"></u-picker>
    <u-picker :show="responsibleIdState" :columns="responsibleList" keyName="label" @confirm="pickerConfirm('responsibleId', $event)" @cancel="pickerCancel('responsibleId')"></u-picker>
  </view>
</template>

<script>
  import { getDangerPlaceListApi, getUserDictListApi, getDeptDictListApi } from '@/apis/danger/common.js'
  import { getUserInfo } from '@/utils/auth.js'
  
  export default {
    data() {
      return {
        list: [],
        params: {
          directPersonId: '',
          responsibleId: '',
          riskPoint: '',
          pageNo: 1,
          pageSize: 10
        },
        directPersonIdName: '',
        responsibleIdName: '',
        popupState: false,
        directPersonIdState: false,
        directPersonList: [],
        responsibleIdState: false,
        responsibleList: [],
        pushState: true
      }
    },
    onLoad(options) {
      if(options.responsibleId && options.directPersonId) Object.assign(this.params, { responsibleId: options.responsibleId, directPersonId: options.directPersonId })
      this.getDangerPlaceList(this.params)
      this.getDeptDictList()
    },
    onReachBottom() {
      if(this.pushState) this.getDangerPlaceList(Object.assign(this.params, {pageNo: this.params.pageNo + 1}), false)
    },
    methods: {
      getDangerPlaceList: function(params, freshState = true) {
        getDangerPlaceListApi(params).then(res => {
          if(freshState) {
            this.pushState = true
            this.list = res.list
          } else {
            this.list = this.list.concat(res.list)
          }
          if(res.list.length < this.params.pageSize) this.pushState = false
          uni.stopPullDownRefresh()
        }).catch(err => console.warn(err))
      },
      getDeptDictList: function() {
        let userInfo = getUserInfo()
        getDeptDictListApi({parentId: userInfo.company.id}).then(res => {
          if(res.length > 0) res.map(item => Object.assign(item, {label: item.name, value: item.id}))
          this.responsibleList = [res]
        }).catch(err => console.warn(err))
      },
      getUserDictList: function(params) {
        getUserDictListApi(params).then(res => {
          if(res.length > 0) res.map(item => Object.assign(item, {label: item.nickname, value: item.id}))
          this.directPersonList = [res]
        }).catch(err => console.warn(err))
      },
      chooseItem: function(item) {
        const wxCurrPage = getCurrentPages()
        const wxPrevPage = wxCurrPage[wxCurrPage.length - 2]
        wxPrevPage.$vm.params.riskId = item.id
        wxPrevPage.$vm.riskName = item.riskPoint
        uni.navigateBack()
      },
      openPicker: function(type) {
        if(this[`${type}List`][0].length) {
          this[`${type}IdState`] = true
        } else {
          uni.showToast({ title: '暂无数据', icon: 'none' })
        }
      },
      openPopup: function() {
        this.popupState = true
      },
      closePopup: function() {
        this.popupState = false
      },
      pickerConfirm: function(type, event) {
        if(type == 'responsibleId') {
          this.params.directPersonId = ''
          this.directPersonIdName = ''
          this.getUserDictList({ deptId: event.value[0].value })
        }
        this.params[type] = event.value[0].value
        this[`${type}Name`] = event.value[0].label
        this[`${type}State`] = false
      },
      pickerCancel: function(type) {
        this[`${type}State`] = false
      },
      submit: function() {
        this.getDangerPlaceList(Object.assign(this.params, { pageNo: 1 }))
      },
      resetForm: function() {
        Object.assign(this.params, {
          directPersonId: '',
          responsibleId: '',
          riskPoint: '',
          pageNo: 1,
          pageSize: 10
        })
        this.directPersonIdName = ''
        this.responsibleIdName = ''
        this.getDangerPlaceList(this.params)
      }
    }
  }
</script>

<style>
  page {
    background-color: #F4F4F4;
  }
  .u-tag {
    padding: 0 !important;
  }
  .u-button {
    background-color: #3F99F7 !important;
  }
</style>

<style lang="scss" scoped>
  .border-b-common { 
    border-bottom: 1rpx solid #F4F4F4; 
  }
  .content {
    padding-top: 150rpx;
    .top-block {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      background-color: #FFFFFF;
      overflow: hidden;
      z-index: 11111;
      border-bottom-left-radius: 20rpx;
      border-bottom-right-radius: 20rpx;
      box-shadow: 0 0 10rpx 0 #F4F4F4;
      .check-block {
        display: flex;
        align-items: center;
        justify-content: space-around;
        .check-item {
          width: 30%;
          font-size: 24rpx;
          line-height: 32rpx;
          color: #3F99F7;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
    .list-block {
      .item-block {
        border-radius: 10rpx;
        background-color: #FFFFFF;
        .info-block {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        .item-title {
          width: 500rpx;
          font-size: 30rpx;
          line-height: 40rpx;
          color: #333333;
          font-weight: 500;
        }
        .state-block {
          border-radius: 6rpx;
          width: 120rpx;
          height: 40rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          color: #3F99F7;
          font-size: 24rpx;
          border: 1rpx solid #3F99F7;
        }
        .item-info {
          font-size: 24rpx;
          line-height: 32rpx;
          color: #999999;
        }
      }
    }
    .popup-block {
      .popup-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .item-label {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #333333;
        }
        .item-select {
          display: flex;
          align-items: center;
          font-size: 28rpx;
          line-height: 40rpx;
          color: #333333;
          .item-default-text {
            color: #999999;
          }
        }
      }
      .button-block {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
    }
    .empty-block {
      padding-top: 300rpx;
    }
  }
</style>